
package com.idealighter.game.games.happyfive.handler;

import com.idealighter.game.core.annotation.ResMsgHandler;
import com.idealighter.game.dictionary.domain.HappyfiveRoomDomain;
import com.idealighter.game.dictionary.domain.RobotConfigDomain;
import com.idealighter.game.gamehall.dto.MemInfo;
import com.idealighter.game.games.happyfive.HappyFivePlayer;
import com.idealighter.game.games.happyfive.message.ReqReadyMsg;
import com.idealighter.game.games.happyfive.message.ResBanlanceMsg;
import com.idealighter.game.message.core.ResMessage;
import com.idealighter.game.robot.core.Player;
import com.idealighter.game.robot.handler.ResMessageHandler;
import com.idealighter.utils.code.RandCodeUtil;

import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@ResMsgHandler(507224)
public class ResBanlanceHandler implements ResMessageHandler {
  private static final Logger LOG = LoggerFactory.getLogger(ResBanlanceHandler.class);

  @Override
  public void action(Player player, ResMessage message) {
    ResBanlanceMsg msg = (ResBanlanceMsg) message;
    HappyFivePlayer happyFivePlayer = (HappyFivePlayer) player;
    happyFivePlayer.resetData();
    msg.getBillInfo().stream()
        .filter(billInfo -> billInfo.getOrder() == happyFivePlayer.seat(player.playerId).getOrder())
        .forEach(billInfo -> {
          happyFivePlayer.member(player.playerId)
              .setChips(happyFivePlayer.member(player.playerId).getChips() + billInfo.getBet());
        });

    MemInfo memInfo = happyFivePlayer.member(player.playerId);

    HappyfiveRoomDomain roomDomain = ((HappyFivePlayer) player).roomDomain;

    // 校验玩家身上的钱换成的筹码是否足够(最小筹码)
    if (memInfo.getChips() < roomDomain.getMinOne()) {
      LOG.info("[欢乐五张]玩家[{}][{}]身上的筹码小于最小筹码", player.userName, player.playerId);
      player.delayLogout();
      return;
    }

    RobotConfigDomain robotCfg = player.configDomain;
    // 超过最大携带金币需要退出房间
    if (convertGold(roomDomain, memInfo.getChips()) > robotCfg.getMaxGold()) {
      LOG.info("[欢乐五张]玩家[{}][{}]身上的金币超过最大携带金币", player.userName, player.playerId,
          robotCfg.getMaxGold());
      player.delayLogout();
      return;
    }

    // 超过最大游戏局数
    if (happyFivePlayer.totalGameNum-- < 1) {
      LOG.info("[欢乐五张]玩家[{}][{}]超过总游戏局数退出游戏...", player.userName, player.playerId);
      player.delayLogout();
      return;
    }

    // 超过换桌局数
    if (happyFivePlayer.tableGameNum-- < 1) {
      happyFivePlayer.tableGameNum =
          RandCodeUtil.random(robotCfg.getTableGameLower(), robotCfg.getTableGameUpper());
      LOG.info("[欢乐五张]玩家[{}][{}]超过换桌局数换桌...", player.userName, player.playerId);
      player.delayLogout();
      return;
    }

    /*
     * if (isExitGame(player)) { player.sendMsg(new ReqLogoutMsg()); player.logout();
     * LOG.info("玩家[{}][{}]退出德州扑克游戏", player.userName, player.playerId); return; }
     */
    // 游戏结束后继续准备
    player.schedule(() -> player.sendMsg(new ReqReadyMsg()), RandCodeUtil.random(3, 5),
        TimeUnit.SECONDS);
  }

  /**
   * 兑换筹码.
   * 
   * @param roomDomain .
   * @param chips .
   */
  public long convertGold(HappyfiveRoomDomain roomDomain, long chips) {
    return chips * roomDomain.getProportionGold() / roomDomain.getProportionChips();
  }


}
